3.3 Operators in C#

What are Operators?

An operator is basically used to perform certain functions/calculations within the program. Example: Assigning some value to a varible with '=' operator.

Types Of Operators

Arithmetic Operator

This type of operator is used do arithmetics within the program.

int a = 2;
int b = 8;
Operator Description Example
+ This operator is used do addition of values. a + b = 10
- This operator is used do substraction of values. a - b = 10
* This operator is used do multiplication of values. a + b = 10
/ This operator is used do division of values. b / a = 4
% This operator known as modulus is used get the remainder. 7 % 2 = 1
++ This operator increments the values a++ = 3
-- This operator decrements the values. a-- = 1

Relational Operator

This type of operator is used to do comparison within the program.

int a = 2;
int b = 4;
Operator Description Example
== Compares 2 values and checks if both the values are equal or not if the values are equal then it returns True. (a==b) is False
!= Compares 2 values and checks if both the values are equal or not if the values are not equal then it returns True. (a!=b) = True
> Compares 2 values and checks if the left value is greater than the right value or not. (b>a) = True
< Compares 2 values and checks if the left value is less than the right value or not. (a<b) = True
>= Compares 2 values and checks if the left value is greater or equals to the right value or not. (b>=a) = True
<= Compares 2 values and checks if the left value is less or equals to the right value or not. (a<=b) = True

Logical Operator

This type of operator is used to do comparison within the program.

int a = 2;
int b = 4;
Operator Description Example
&& Called Logical AND operator. If both the operands conditions are true then it returns True (a == 2 && b == 4) = True
|| Called Logical OR operator. If any of the operands condition is true then it returns True (a == 2 || b == 2) = True
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false !(a == 2 && b == 4) = False

Relational Operator

This type of operator is used to do comparison within the program.

int a = 2;
int b = 4;
Operator Description Example
= Used to assign a value to a variable c = a + b
+= It adds right operand to the left operand and assign the result to left operand a+=1 = 3
-= It substracts right operand to the left operand and assign the result to left operand a-=1 = 1
*= It multiplies right operand to the left operand and assign the result to left operand a*=2 = 2
/= It divides right operand to the left operand and assign the result to left operand b/=2 = 2
%= It takes modulus using two operands and assign the result to left operand b%=2 = 0

Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation.

Operator Precedence

This defines the importance of operators being used just like BODMAS rule in mathematics. The operator with higher precedence is done at first.